package cluster

import akka.actor.{ActorRef, ActorSystem, Kill, Props}
import com.typesafe.config.ConfigFactory

import scala.collection.mutable
import scala.collection.mutable.HashMap

/**
  * Created by shouf on 2016/5/17.
  */
object ClusterMain extends App{
//  val system = ActorSystem("clusterSys", ConfigFactory.load().getConfig("cluster"))

  val actors = HashMap[Int, ActorRef]()
  if(args.isEmpty){
    startup(Seq(2751, 2752, 2753))
  }
  else{
    val seq: Seq[Int] = args.map(_.toInt).toSeq
    startup(seq)
  }

  /**
    * start actors, if the actor in the config of akka.cluster.seed-nodes will join the cluster
    * @param ports
    */
  def startup(ports: Seq[Int]): Unit = {
    ports foreach { port =>
      // Override the configuration of the port
      val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=${port}").
        withFallback(ConfigFactory.load().getConfig("cluster")) //withFallback的意思是附加其他配置信息

      // Create an Akka system
      val system = ActorSystem("clusterSys", config)
      // Create an actor that handles cluster domain events
      actors(port) = system.actorOf(Props[ClusterActor], name = "clusterListener")
//      println(s"path:${actor.path}")
//      actor ! "wsf"
    }
  }

//  seq foreach { s =>
//    actors(s) ! s"actor port$s send wsf$s"
//    println(s"Port is $s,path is ${actors(s).path}")
////    actors(s) ! Kill
////    actors(s) ! s"killed ${s}"
//  }
}
